﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.BLL.StorageForecast;
using StoragePeriod.Model.StorageForecast;


namespace StoragePeriod.Web.StorageForecast.StoragePredictions
{
    public partial class StoragePredictionsEdit : System.Web.UI.Page
    {
        T_StorageCalculateMove tStorageCalculateMove = new T_StorageCalculateMove(); //计算公式

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["RawID"] != null && Request.QueryString["NormID"] != null)
                {
                    this.LoadData();
                }
            }
        }

        //加载数据
        private void LoadData()
        {
            int RawID = int.Parse(Request.QueryString["RawID"].ToString()); //原料ID
            int NormID = int.Parse(Request.QueryString["NormID"].ToString());//极限值ID

            E_StorageCalculateMove eStorageCalculateMove = tStorageCalculateMove.GetModelList("RawID=" + RawID + " and NormID=" + NormID).FirstOrDefault();
            if (eStorageCalculateMove != null)
            {
                this.txt_M.Text = eStorageCalculateMove.M.ToString();
                this.txt_N.Text = eStorageCalculateMove.N.ToString();
                this.txt_Z.Text = eStorageCalculateMove.Z.ToString();
            }
        }

        //提交数据
        protected void btPost_Click(object sender, EventArgs e)
        {
            int RawID = int.Parse(Request.QueryString["RawID"].ToString());
            int NormID = int.Parse(Request.QueryString["NormID"].ToString());
            bool IsAdd = false;

            E_StorageCalculateMove eStorageCalculateMove = tStorageCalculateMove.GetModelList("RawID=" + RawID + " and NormID=" + NormID).FirstOrDefault();
            if (eStorageCalculateMove == null)
            {
                eStorageCalculateMove = new E_StorageCalculateMove();
                IsAdd = true;
            }
            eStorageCalculateMove.RawID = RawID;
            eStorageCalculateMove.NormID = NormID;
            eStorageCalculateMove.M = decimal.Parse(this.txt_M.Text);
            eStorageCalculateMove.N = decimal.Parse(this.txt_N.Text);
            eStorageCalculateMove.Z = decimal.Parse(this.txt_Z.Text);
            if (IsAdd)
            {
                tStorageCalculateMove.Add(eStorageCalculateMove);
            }
            else
            {
                tStorageCalculateMove.Update(eStorageCalculateMove);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('保存成功！');</script>");
        }
    }
}